﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMK.Framework.Model;
using XMK.Tadong.Data;
using XMK.Utils;
using XMK.Tadong.Model;
using XMK.Tadong.Model.Cond;
using System.Data;
using System.Web;
namespace XMK.Tadong.Biz
{
  public  class ZnkzBiz
    {
      /// <summary>
      /// 获取主题
      /// </summary>
      /// <param name="userId"></param>
      /// <returns></returns>
      public DataTable getUserSubject(string userId) {
          try
          {
               userId = LoginUserBiz.LoginUserID();
               ZnkzData data = new ZnkzData(Config.TadongDbProvider);
               DataTable table = data.getUserSubject(userId);
               return table; 
               
           }
          catch (Exception ex)
          {
              //生成返回对象（带错误信息）
              
          }
          return null;
      }

      /// <summary>
      /// 根据查询语句搜索扩展词
      /// </summary>
      /// <param name="userId"></param>
      /// <returns></returns>
      public DataTable getExpanWordsBySql(string sqlStr)
      {
          try
          {
              ZnkzData data = new ZnkzData(Config.TadongDbProvider);
              DataTable table = data.getExpanWordsBySql(sqlStr);
              return table;

          }
          catch (Exception ex)
          {
              //生成返回对象（带错误信息）

          }
          return null;
      }

      /// <summary>
      /// 主题列表
      /// </summary>
      /// <returns></returns>
      public ResponseInfo QuerySubjectList(PageInfo pageInfo, OrderInfo order)
      {
          ResponseInfo response = null;
          try
          {
              ZnkzData data = new ZnkzData(Config.TadongDbProvider);
              response = new ResponseInfo();
              response.Data = (List<Tb_SubjectInfo>)data.QuerySubjectList(pageInfo, order);
              response.Page = pageInfo;
          }
          catch (Exception ex)
          {
              //生成返回对象（带错误信息）
              response = new ResponseInfo("error！");
              LogHelper.WriteLog(LogType.Error, "[QuerySubjectList:]" + ex.ToString());
          }
          return response;

      }
      /// <summary>
      /// 根据id获取主题数据
      /// </summary>
      /// <param name="ext_id"></param>
      /// <returns></returns>
      public ResponseInfo InnitSubject(string id)
      {
          ResponseInfo response = new ResponseInfo();
          ZnkzData data = new ZnkzData(Config.TadongDbProvider);
          response.Data = (Tb_SubjectInfo)data.getSubjectById(id);
          return response;
      }
      /// <summary>
      /// 根据id获取主题分类
      /// </summary>
      /// <param name="ext_id"></param>
      /// <returns></returns>
      public ResponseInfo getThemeClassById(string id)
      {
          
          ResponseInfo response = new ResponseInfo();
          ZnkzData data = new ZnkzData(Config.TadongDbProvider);
          response.Data = (ThemeClass)data.getThemeClassById(id);
          return response;
      }
      /// <summary>
      /// 根据id获取主题分类列表
      /// </summary>
      /// <param name="ext_id"></param>
      /// <returns></returns>
      public ResponseInfo getThemeClass(ThemeClass cond, PageInfo page, OrderInfo order)
      {

          ResponseInfo response = new ResponseInfo();
          ZnkzData data = new ZnkzData(Config.TadongDbProvider);
          response.Data = (List<ThemeClass>)data.getThemeClass(cond, page, order);
          response.Page = page;
          return response;
      }

      /// <summary>
      /// 根据用户id获取对应主题列表
      /// </summary>
      /// <param name="userId"></param>
      /// <returns></returns>
      public string QueryThemeByThemeTypeId(string themeTypeId)
      {
          string jsonResult = "";
          try
          {
              DataColumn dc = new DataColumn();
              dc.DataType = typeof(System.Boolean);
              dc.ColumnName = "checked";
              //dc.ColumnName = "checked";
              ZnkzData data = new ZnkzData(Config.TadongDbProvider);
              DataSet ds = data.QueryThemeByThemeTypeId(themeTypeId);
              DataTable dtUserTheme = ds.Tables[1];
              DataTable dt = ds.Tables[0];
              dt.Columns.Add(dc);
              for (int i = 0; i < dt.Rows.Count; i++)
              {
                  DataRow dr = dt.Rows[i];
                  DataRow[] rows = dtUserTheme.Select("SUBJ_ID='" + Convert.ToString(dr["SUBJ_ID"]) + "'");
                  if (rows.Length > 0)
                  {
                      dt.Rows[i]["checked"] = true;
                  }
                  else
                  {
                      dt.Rows[i]["checked"] = false;
                  }
              }
              jsonResult = JsonHelper.ToJson(dt);
          }
          catch (Exception ex)
          {
              string str = ex.Message;
          }


          return jsonResult;
      }

      /// <summary>
      /// 根据用户id获取对应主题类别列表
      /// </summary>
      /// <param name="userId"></param>
      /// <returns></returns>
      public string QueryThemeTypeByUser(string userId)
      {
          string jsonResult = "";
          try
          {
              DataColumn dc = new DataColumn();
              dc.DataType = typeof(System.Boolean);
              dc.ColumnName = "checked";
              //dc.ColumnName = "checked";
              ZnkzData data = new ZnkzData(Config.TadongDbProvider);
              DataSet ds = data.QueryThemeTypeByUser(userId);
              DataTable dtUserTheme = ds.Tables[1];
              DataTable dt = ds.Tables[0];
              dt.Columns.Add(dc);
              for (int i = 0; i < dt.Rows.Count; i++)
              {
                  DataRow dr = dt.Rows[i];
                  DataRow[] rows = dtUserTheme.Select("SUBJ_CLASS_ID='" + Convert.ToString(dr["SUBJ_CLASS_ID"]) + "'");
                  if (rows.Length > 0)
                  {
                      dt.Rows[i]["checked"] = true;
                  }
                  else
                  {
                      dt.Rows[i]["checked"] = false;
                  }
              }
              jsonResult = JsonHelper.ToJson(dt);
          }
          catch (Exception ex)
          {
              string str = ex.Message;
          }


          return jsonResult;
      }

      /// <summary>
      /// 新增主题
      /// </summary>
      /// <param name="cond"></param>
      /// <returns></returns>
      public ResponseInfo themeAdd(Tb_SubjectInfo cond)
      {
          ResponseInfo response = null;
          try
          {

              ZnkzData data = new ZnkzData(Config.TadongDbProvider);
              response = new ResponseInfo();
              
              if (cond.SUBJ_ID != null)
              {
                  response.Data = (Tb_SubjectInfo)data.getSubjectById(cond.SUBJ_ID);
                  if (response.Data != null)
                  {

                      response.Data = (bool)data.UpdateSubject(cond);
                  }
              }
             else
              {
                  response.Data = (bool)data.themeAdd(cond);
              }

          }
          catch (Exception ex)
          {
              //生成返回对象（带错误信息）
              response = new ResponseInfo("error！");
              LogHelper.WriteLog(LogType.Error, "[ExtTypeAdd:]" + ex.ToString());
          }
          return response;

      }

      /// <summary>
      /// 新增主题类别
      /// </summary>
      /// <param name="cond"></param>
      /// <returns></returns>
      public ResponseInfo themeTypeAdd(ThemeClass cond)
      {
          ResponseInfo response = null;
          try
          {

              ZnkzData data = new ZnkzData(Config.TadongDbProvider);
              response = new ResponseInfo();
              response.Data = (Tb_SubjectInfo)data.getThemeTypeByName(cond.SUBJ_CLASS_NAME);
              if (response.Data != null)
              {
                  response.Data = true;
                  //response.Data = (bool)data.UpdateThemeType(cond);
              }
              else if (cond.SUBJ_CLASS_ID == "")
              {
                  response.Data = (bool)data.themeTypeAdd(cond);
              }
              else
              {
                  response.Data = (bool)data.UpdateThemeType(cond);
              }

          }
          catch (Exception ex)
          {
              //生成返回对象（带错误信息）
              response = new ResponseInfo("error！");
              LogHelper.WriteLog(LogType.Error, "[ExtTypeAdd:]" + ex.ToString());
          }
          return response;

      }
      /// <summary>
      /// 删除主题类别
      /// </summary>
      /// <param name="cond"></param>
      /// <returns></returns>
      public ResponseInfo deleteThemeType(ThemeClass cond)
      {
          ResponseInfo response = null;
          try
          {

              ZnkzData data = new ZnkzData(Config.TadongDbProvider);
              response = new ResponseInfo();
              response.Data = (bool)data.deleteThemeType(cond);

          }
          catch (Exception ex)
          {
              //生成返回对象（带错误信息）
              response = new ResponseInfo("error！");
              LogHelper.WriteLog(LogType.Error, "[EditDictType:]" + ex.ToString());
          }
          return response;

      }

      /// <summary>
      /// 删除
      /// </summary>
      /// <param name="cond"></param>
      /// <returns></returns>
      public ResponseInfo deleteSub(Tb_SubjectInfo cond)
      {
          ResponseInfo response = null;
          try
          {

              ZnkzData data = new ZnkzData(Config.TadongDbProvider);
              response = new ResponseInfo();
              response.Data = (bool)data.deleteSub(cond);
              
          }
          catch (Exception ex)
          {
              //生成返回对象（带错误信息）
              response = new ResponseInfo("error！");
              LogHelper.WriteLog(LogType.Error, "[EditDictType:]" + ex.ToString());
          }
          return response;

      }
    

      /// <summary>
      /// 批量修改用户主题类别
      /// </summary>
      /// <param name="paras"></param>
      /// <returns></returns>
      public ResponseInfo EditUserThemeTypeInfo(string paras)
      {
          ResponseInfo response = new ResponseInfo();

          string[] paraAtt = paras.Split(',');
          string userid = paraAtt[0];

          ZnkzData data = new ZnkzData(Config.TadongDbProvider);
          //先删除去掉的数据
          bool confirm = data.deleteUserThemeTypeByUserId(paras);

          //插入新增加的数据
          for (int i = 1; i < paraAtt.Length; i++)
          {
              response.Data = data.AddUserThemeTypeInfo(userid, paraAtt[i].ToString());
             
          }

          return response;
      }

      /// <summary>
      /// 批量修改主题类别-主题
      /// </summary>
      /// <param name="paras"></param>
      /// <returns></returns>
      public ResponseInfo EditThemeType_themeInfo(string paras)
      {
          ResponseInfo response = new ResponseInfo();

          string[] paraAtt = paras.Split(',');
          string themetypeid = paraAtt[0];

          ZnkzData data = new ZnkzData(Config.TadongDbProvider);
          //先删除去掉的数据
          bool confirm = data.deleteThemeByThemeId(paras);

          //插入新增加的数据
          for (int i = 1; i < paraAtt.Length; i++)
          {
              response.Data = data.AddThemeType_ThemeInfo(themetypeid, paraAtt[i].ToString());

          }

          return response;
      }
    }
}
